package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.feign.ProductsFeign;
import com.lagou.pojo.Orders;
import com.lagou.pojo.OrdersVO;
import com.lagou.pojo.Products;
import com.lagou.pojo.ResponseResult;
import com.lagou.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/page")
public class PageController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/query/{id}")
    public Orders queryById(@PathVariable Integer id){
        return  orderService.queryById (id);
    }

    @GetMapping("/queryByPage")
    public ResponseResult queryByPage(@RequestBody OrdersVO ordersVO) {
        PageInfo<Orders> pageInfo = orderService.queryByPage (ordersVO);
        //封装数据
        System.out.println (pageInfo);
        ResponseResult responseResult = new ResponseResult ();
        responseResult.setMessage ("查询成功");
        responseResult.setStatus (200);
        responseResult.setSuccess (true);
        HashMap<String, Object> map = new HashMap<> ();
        map.put ("list",pageInfo.getList ());
        map.put ("total",pageInfo.getTotal ());
        responseResult.setContent (map);
        return responseResult;
    }

    @Autowired
    private ProductsFeign productsFeign;

    @GetMapping("/getProductByOrder/{id}")
    public ResponseResult getProductByOrder(@PathVariable Integer id){
        List<Integer> productIdList = orderService.getProductIdListByOrderId (id);
        System.out.println (productIdList + "productIdList");
        ArrayList<Products> productsList = new ArrayList<> ();
        for (Integer i:productIdList){
            System.out.println ("i =" +  i);
            productsList.add (productsFeign.queryById (i));
        }
        //封装数据
        ResponseResult responseResult = new ResponseResult ();
        responseResult.setMessage ("查询成功");
        responseResult.setStatus (200);
        responseResult.setSuccess (true);
        responseResult.setContent (productsList);
        return responseResult;
    }
}
